﻿using ServerCommon.Config;
using ServerCommon.Log;
using ServerCommon.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*
 * 
	"NetConfigure": {
		"GameNetConfig": {},
		"WorldNetConfig": {
			"GameService": {
				"MaxConnection": 40,
				"GlobalMaxConnection": 80,
				"SAEABufferSize": 2097152
			}
		},
		"DatabaseNetConfig": {
			"GameService": {
				"MaxConnection": 40,
				"SAEABufferSize": 2097152
			}
		},
		"GateNetConfig": {
			"GameService": {
				"MaxConnection": 40,
				"SAEABufferSize": 2097152
			},
			"ClientService": {
				"MaxConnection": 5000,
				"SAEABufferSize": 8192
			}
		},
		"ServerCustomConfig": {
			"MaxConnection": 6,
			"SAEABufferSize": 2097152
		}
	},
*/
namespace DatabaseServer.Server
{
    public class DatabaseNet
    {
        public static readonly DatabaseNet Instance = new Lazy<DatabaseNet>(() => new DatabaseNet(), true).Value;
        private static LogHelper log = LogHelper.GetLogger(typeof(DatabaseNet));

        NetNodeManager _managerDatabase;
        NetService _serviceDatabase;

		NetNodeManager _managerGM;
		NetClient _clientGM;

        public void Init()
        {
            _managerDatabase = new NetNodeManager();
            _managerDatabase.Init();

			_managerGM = new NetNodeManager();
			_managerGM.Init();

			var netConfig = ConfigManager.Instance.NetConfig["NetConfigure"];
			var dataNetConfig = netConfig["DatabaseNetConfig"];
			var serviceNetConfig = dataNetConfig["GameService"];
            int connectionCount = serviceNetConfig.Value<int>("MaxConnection");
            int bufferSize = serviceNetConfig.Value<int>("SAEABufferSize");

			var serverConfig = ConfigManager.Instance.ServerConfig;
			var dataConfig = serverConfig["DatabaseServer"];
            string ip = dataConfig.Value<string>("Ip");
            int port = dataConfig.Value<int>("Port");
			long localServerId = dataConfig.Value<long>("Id");

            _serviceDatabase = new NetService();
			_serviceDatabase.Config(_managerDatabase, EnumNodeServiceType.Database, ip, port, connectionCount, bufferSize, localServerId);
			_serviceDatabase.Start();
			_serviceDatabase.ShowPrompt();

			var clientNetConfig = netConfig["ServerCustomConfig"];
            connectionCount = clientNetConfig.Value<int>("MaxConnection");
            bufferSize = clientNetConfig.Value<int>("SAEABufferSize");

            var gmConfig = serverConfig["GMServer"];
            ip = gmConfig.Value<string>("Ip");
            port = gmConfig.Value<int>("Port");
			long gmServerId = gmConfig.Value<long>("Id");

            _clientGM = new NetClient();
            _clientGM.Config(_managerGM, ip, port, connectionCount, bufferSize, gmServerId, localServerId);
            _clientGM.Start();
            _clientGM.ShowPrompt();

			NetNodeReconnect.Instance.Init();
        }

        public void Run()
        {
			_managerDatabase.DoNetMessages();
			_managerGM.DoNetMessages();
        }

        public void Stop()
        {
            NetNodeReconnect.Instance.Release();

            _managerDatabase.StopNetwork();
			_managerGM.StopNetwork();
        }

        public void Release()
        {
        }
    }
}
